package me.cxlr.shaopinghelper.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
import androidx.core.content.res.ResourcesCompat;

import me.cxlr.shaopinghelper.R;

/**
 * 控件--卡片式显示组件
 *
 * @author ShanLunZhi
 */
public class CardViewTipsWidget extends LinearLayoutCompat {
    private Context context;
    private View view;
    private CardView cardView;
    private LinearLayoutCompat llcRoot;
    private AppCompatImageView imageView;
    private AppCompatTextView textViewTitle;
    private LinearLayoutCompat llcAddPoint;

    public CardViewTipsWidget(@NonNull Context context) {
        super(context);
        this.context = context;
        initWidget();
    }

    public CardViewTipsWidget(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.context = context;
        initWidget();
    }

    private void initWidget() {
        view = LayoutInflater.from(context).inflate(R.layout.widget_cardview_tips, this);
        cardView = view.findViewById(R.id.wct_cv);
        llcRoot = view.findViewById(R.id.wct_ll_root);
        imageView = view.findViewById(R.id.wct_img);
        textViewTitle = view.findViewById(R.id.wct_tv_title);
        llcAddPoint = view.findViewById(R.id.wct_llc_add_point);

        cardView.setRadius(35F);
    }

    public void setImage(int resourceId) {
        Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), resourceId, null);
        if (drawable != null) {
            imageView.setImageDrawable(drawable);
        }
    }

    public void setImage(Drawable drawable) {
        if (drawable != null) {
            imageView.setImageDrawable(drawable);
        }
    }

    public void setTitle(int resourceId) {
        textViewTitle.setText(resourceId);
    }

    public void setTitle(CharSequence title) {
        textViewTitle.setText(title);
    }

    public void setMessage(int resourceId) {
        LinearLayoutCompat llTmp = new LinearLayoutCompat(context);
        LinearLayoutCompat.LayoutParams layoutParams = new LinearLayoutCompat.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, 0, 0);
        llTmp.setLayoutParams(layoutParams);

        AppCompatTextView textView = new AppCompatTextView(context);
        textView.setTextSize(16);
        llTmp.addView(textView);

        llcAddPoint.addView(llTmp);
    }

    public void setMessage(CharSequence title) {
        LinearLayoutCompat llTmp = new LinearLayoutCompat(context);
        LinearLayoutCompat.LayoutParams layoutParams = new LinearLayoutCompat.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 0, 0, 0);
        llTmp.setLayoutParams(layoutParams);

        AppCompatTextView textView = new AppCompatTextView(context);
        textView.setTextSize(16);
        textView.setText(title);
        llTmp.addView(textView);

        llcAddPoint.addView(llTmp);
    }

    public void flush(){
        llcAddPoint.removeAllViews();
    }
}
